package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderStoreAdidasBo;
import com.ruoyi.system.domain.vo.KfnWorkOrderStoreAdidasVo;
import com.ruoyi.system.domain.KfnWorkOrderStoreAdidas;
import com.ruoyi.system.mapper.KfnWorkOrderStoreAdidasMapper;
import com.ruoyi.system.service.IKfnWorkOrderStoreAdidasService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工单Service业务层处理
 *
 * @author Panda
 * @date 2023-12-21
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderStoreAdidasServiceImpl implements IKfnWorkOrderStoreAdidasService {

    private final KfnWorkOrderStoreAdidasMapper baseMapper;

    /**
     * 查询工单
     */
    @Override
    public KfnWorkOrderStoreAdidasVo queryById(Long storeId){
        return baseMapper.selectVoById(storeId);
    }

    /**
     * 查询工单列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderStoreAdidasVo> queryPageList(KfnWorkOrderStoreAdidasBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderStoreAdidas> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderStoreAdidasVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工单列表
     */
    @Override
    public List<KfnWorkOrderStoreAdidasVo> queryList(KfnWorkOrderStoreAdidasBo bo) {
        LambdaQueryWrapper<KfnWorkOrderStoreAdidas> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnWorkOrderStoreAdidas> buildQueryWrapper(KfnWorkOrderStoreAdidasBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderStoreAdidas> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStoreNumber()), KfnWorkOrderStoreAdidas::getStoreNumber, bo.getStoreNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), KfnWorkOrderStoreAdidas::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), KfnWorkOrderStoreAdidas::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistricts()), KfnWorkOrderStoreAdidas::getDistricts, bo.getDistricts());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), KfnWorkOrderStoreAdidas::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), KfnWorkOrderStoreAdidas::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), KfnWorkOrderStoreAdidas::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreBrand()), KfnWorkOrderStoreAdidas::getStoreBrand, bo.getStoreBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreStatus()), KfnWorkOrderStoreAdidas::getStoreStatus, bo.getStoreStatus());
        return lqw;
    }

    /**
     * 新增工单
     */
    @Override
    public Boolean insertByBo(KfnWorkOrderStoreAdidasBo bo) {
        KfnWorkOrderStoreAdidas add = BeanUtil.toBean(bo, KfnWorkOrderStoreAdidas.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStoreId(add.getStoreId());
        }
        return flag;
    }

    /**
     * 修改工单
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderStoreAdidasBo bo) {
        KfnWorkOrderStoreAdidas update = BeanUtil.toBean(bo, KfnWorkOrderStoreAdidas.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderStoreAdidas entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
